We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Model->writeAttribute() Call to undefined method

I'm trying to do some CRUD operations and I want to set the models properties by iterating over a values array using the writeAttribute method but i get this error message everytime: PHP Fatal error: Call to undefined method Phalcon\Mvc\Model\Resultset\Simple::writeAttribute()

anyone sees the fault?

public function editAction($id){

$c = Countries::findById($id);

$arr = $this->request->getRawBody();
$arr = json_decode($arr, true);

foreach($arr as $k => $v ){ $c->writeAttribute($k, $v); }

$c->save();

}

Also, is this way secure enough? or it will misbehabe if some field in the JSON don't exist in the model?

Thanks in advance.



1.4k

Okk! My fault! i was tring to do writeAttribute over the resultset, no the actual model.

so changing:

$c = Countries::findById($id);

for:

$c = Countries::findFirstById($id);

did the trick

Also instead of json_decode true just put true in getjsonrawbody